<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Messages_c extends CI_Controller {
        function __construct() {
            parent::__construct();
            $this->load->model(array('messages_m', 'coordinator_m'));
        }
        
        function index() {
            $data['content'] = 'pages/messages/messages';
            $data['title'] = 'Messages';
            $data['current_page'] = 'inbox-page';
            //$data['inbox_num'] = $this->messages_m->get_inbox_num($this->session->userdata('accid'));
            //$data['sentbox_num'] = $this->messages_m->get_sentbox_num($this->session->userdata('accid'));
            $data['inbox'] = $this->messages_m->get_inbox($this->session->userdata('accid'));
            $this->load->view('includes/template', $data);
        }
        
        function count_inbox_sentbox() {
            $data = array();
            $data['inbox_num'] = $this->messages_m->get_inbox_num($this->session->userdata('accid'));
            $data['sentbox_num'] = $this->messages_m->get_sentbox_num($this->session->userdata('accid'));
            echo json_encode($data);
        }
        
        function recipients() {
            echo json_encode($this->coordinator_m->get_recipients($this->session->userdata('accid')));
        }
        
        function sentbox() {
            $data['content'] = 'pages/messages/sentbox';
            $data['title'] = 'Sentbox Messages';
            $data['current_page'] = 'sentbox-page';
            $data['class'] = 'messages-page';
            $data['back_to'] = 'dashboard';
            $data['inbox_num'] = $this->messages_m->get_inbox_num($this->session->userdata('accid'));
            $data['sentbox_num'] = $this->messages_m->get_sentbox_num($this->session->userdata('accid'));
            $result = $this->messages_m->get_sentbox($this->session->userdata('accid'));
            $data['result'] = $result;
            if($result !== FALSE) {
                foreach ($result as $row) {
                    $recipient = $this->coordinator_m->get_sender_or_recipient($row['recipient']);
                }
                $data['recipient'] = $recipient;
            }
            $this->load->view('includes/template', $data);
        }
        
        function delete_message() {
            if($this->input->post('from')==='inbox'){
                $this->messages_m->delete_msg_inbox($this->session->userdata('accid'), 
                    $this->input->post('msg_id'));
            }elseif($this->input->post('from')==='sentbox'){
                $this->messages_m->delete_msg_sentbox($this->session->userdata('accid'), 
                    $this->input->post('msg_id'));
            }
        }
        
        function sender($acc_id) {
            echo json_encode($this->coordinator_m->get_sender_or_recipient($acc_id));
        }
        
        function message($msg_id) {
            echo(json_encode($this->messages_m->get_message($msg_id)));
        }
        
        function send_message() {
            $msg_action = $this->input->post('msg_action');
            $file_element_name = '';
            $status = '';
            $msg = '';
            if($msg_action=='0'){
                if($this->input->post('origin')=='0'){
                    $file_element_name = 'n-i-userfile';
                }else{
                    $file_element_name = 'n-s-userfile';
                }
            }elseif($msg_action=='1'){
                if($this->input->post('origin')=='0'){
                    $file_element_name = 'f-i-userfile';
                }else{
                    $file_element_name = 'f-s-userfile';
                }
            }elseif($msg_action=='2'){
                if($this->input->post('origin')=='0'){
                    $file_element_name = 'r-i-userfile';
                }else{
                    $file_element_name = 'r-s-userfile';
                }
            }
            if($this->input->post('msg_type')=='1'){
                $config['upload_path'] = './assets/uploads/attachments';
                $config['allowed_types'] = '*';
                $this->load->library('upload', $config);
                
                if (!$this->upload->do_upload($file_element_name)){
                   $status = 'error';
                   $msg = $this->upload->display_errors('', '');
                }else{
                   $upload_data = $this->upload->data();
                   $msg_data = array(
                    'subject' => $this->input->post('subject'),
                    'msg_body' => $this->input->post('msg_body'),
                    'date' => $this->input->post('date_time'),
                    'sender' => $this->session->userdata('accid'),
                    'recipient' => $this->input->post('recipient'),
                    'message_status' => '0',
                    'read' => '0',
                    'attach_name' => $upload_data['file_name'],
                    'attach_path' => 'assets/uploads/attachments/'.$upload_data['file_name'],
                );
                   $id = $this->messages_m->insert_msg($msg_data);
                   if($id){
                      $status = 'success';
                   }else{
                      unlink($upload_data['full_path']);
                      $status = "error";
                      $msg = 'Something went wrong when saving the file, please try again.';
                   }
                }
                @unlink($_FILES[$file_element_name]);
            }else{
                $msg_data = array(
                    'subject' => $this->input->post('subject'),
                    'msg_body' => $this->input->post('msg_body'),
                    'date' => $this->input->post('date_time'),
                    'sender' => $this->session->userdata('accid'),
                    'recipient' => $this->input->post('recipient'),
                    'message_status' => '0',
                    'read' => '0',
                    'attach_name' => '',
                    'attach_path' => '',
                );
                $id = $this->messages_m->insert_msg($msg_data);
                if($id){
                    $status = 'success';
                       
                }else{
                    $status = 'error';
                }
            }
            echo json_encode(array('status' => $status, 'msg' => $msg));
            
//            if ($this->form_validation->run() == FALSE) {
//                $this->index();
//            }else {
//                if($this->input->post('form-name') == 'new-message') {
//                    $num = $this->messages_m->get_max_msg_id();
//                    $msg_id = $num->msg_id + 1;
//                    $recipient = explode('-', $this->input->post('recipient'));
//                    $data = array(
//                        'msg_id' => $msg_id,
//                        'subject' => $this->input->post('subject'),
//                        'msg_body'=> $this->input->post('message_body'),
//                        'date' => date('Y-m-d'),
//                    );
//
//                    $this->messages_m->insert_message($data);
//                    
//                    if ($this->upload->do_upload()) {
//                        $upload_data = $this->upload->data();
//                        $msg_sender = array(
//                            'msg_id' => $msg_id,
//                            'sender' => $this->session->userdata('accid'),
//                            'recipient' => $recipient[1],
//                            'message_status' => 0,
//                            'read' => 1,
//                            'attach_path' => 'assets/uploads/attachments/' . $upload_data['file_name'],
//                            'attach_name' => $upload_data['file_name'],
//                        );
//                    }else{
//                        $msg_sender = array(
//                            'msg_id' => $msg_id,
//                            'sender' => $this->session->userdata('accid'),
//                            'recipient' => $recipient[1],
//                            'message_status' => 0,
//                            'read' => 1,
//                        );
//                    }
//
//                    $this->messages_m->insert_to_user_messages($msg_sender);
//                    if($this->input->post('inbox-sentbox') == 'inbox') {
//                        $this->index('<p class="success">Message successfully sent.</p>');
//                    }elseif($this->input->post('inbox-sentbox') == 'sentbox') {
//                        $this->sentbox('<p class="success">Message successfully sent.</p>');
//                    }
//                } elseif($this->input->post('form-name') == 'reply-message') {
//                    $num = $this->messages_m->get_max_msg_id();
//                    $msg_id = $num->msg_id + 1;
//                    $data = array(
//                        'msg_id' => $msg_id,
//                        'subject' => $this->input->post('subject'),
//                        'msg_body'=> $this->input->post('message_body'),
//                        'date' => date('Y-m-d')
//                    );
//
//                    $this->messages_m->insert_message($data);
//                    
//                    if ($this->upload->do_upload()) {
//                        $upload_data = $this->upload->data();
//                        $msg_sender = array(
//                            'msg_id' => $msg_id,
//                            'sender' => $this->session->userdata('accid'),
//                            'recipient' => $this->input->post('account-id'),
//                            'message_status' => 0,
//                            'read' => 1,
//                            'attach_path' => 'assets/uploads/attachments/' . $upload_data['file_name'],
//                            'attach_name' => $upload_data['file_name'],
//                        );
//                    }else{
//                        $msg_sender = array(
//                            'msg_id' => $msg_id,
//                            'sender' => $this->session->userdata('accid'),
//                            'recipient' => $this->input->post('account-id'),
//                            'message_status' => 0,
//                            'read' => 1,
//                        );
//                        
//                    }
//                    $this->messages_m->insert_to_user_messages($msg_sender);
//
//                    $this->index('<p class="success">Message successfully sent.</p>');
//                } elseif($this->input->post('form-name') == 'forward-message') {
//                    $num = $this->messages_m->get_max_msg_id();
//                    $msg_id = $num->msg_id + 1;
//                    $recipient = explode('-', $this->input->post('recipient'));
//                    $data = array(
//                        'msg_id' => $msg_id,
//                        'subject' => $this->input->post('subject'),
//                        'msg_body'=> $this->input->post('message_body'),
//                        'date' => date('Y-m-d')
//                    );
//
//                    $this->messages_m->insert_message($data);
//                    if ($this->upload->do_upload()) {
//                        $upload_data = $this->upload->data();
//                        $msg_sender = array(
//                            'msg_id' => $msg_id,
//                            'sender' => $this->session->userdata('accid'),
//                            'recipient' => $recipient[1],
//                            'message_status' => 0,
//                            'read' => 1,
//                            'attach_path' => 'assets/uploads/attachments/' . $upload_data['file_name'],
//                            'attach_name' => $upload_data['file_name'],
//                        );
//                    }else{
//                        $msg_sender = array(
//                            'msg_id' => $msg_id,
//                            'sender' => $this->session->userdata('accid'),
//                            'recipient' => $recipient[1],
//                            'message_status' => 0,
//                            'read' => 1,
//                        );
//                    }
//                    $this->messages_m->insert_to_user_messages($msg_sender);
//
//                    if($this->input->post('inbox-sentbox') == 'inbox') {
//                        $this->index('<p class="success">Message successfully sent.</p>');
//                    }elseif($this->input->post('inbox-sentbox') == 'sentbox') {
//                        $this->sentbox('<p class="success">Message successfully sent.</p>');
//                    }
//                }
//            }
        }
        
        function change_status($id) {
            $this->messages_m->change_status($this->session->userdata('accid'), $id);
        }
    }
